#include "AbsoluteMouseController.h"

AbsoluteMouseController::AbsoluteMouseController(controllers::Joystick* cont):
	MouseController(cont) {
		Dimension screenSize;
		EnumDisplayMonitors(NULL, NULL, &monitorCallback, reinterpret_cast<LPARAM>(&screenSize));
		centerX = screenSize.width / 2;
		centerY = screenSize.height / 2;
		oldX = centerX;
		oldY = centerY;
}


AbsoluteMouseController::AbsoluteMouseController(AbsoluteMouseController& orig): 
	MouseController(orig),
	centerX(orig.centerX),
	centerY(orig.centerY),
	oldX(orig.oldX),
	oldY(orig.oldY) {}

AbsoluteMouseController::~AbsoluteMouseController() {}

void AbsoluteMouseController::run() {
	int newX = (int) ((controller->getX() + 1) * centerX);
	int newY = (int) ((controller->getY() + 1) * centerY);
	// If the new position is equal to the old postiton, don't move the 
	// mouse cursor, so it is not impossible to move it with the real mouse
	if (newX != oldX || newY != oldY) {
		robot.mouseMove(newX, newY);
	}
	oldX = newX;
	oldY = newY;
}

//std::vector<RECT> AbsoluteMouseController::monitors;

BOOL CALLBACK AbsoluteMouseController::monitorCallback(_In_  HMONITOR hMonitor, _In_  HDC hdcMonitor, _In_  LPRECT lprcMonitor, _In_  LPARAM dwData){
	Dimension* screenSize = reinterpret_cast<Dimension*>(dwData);
	MONITORINFO monInfo;
	monInfo.cbSize = sizeof(MONITORINFO);

	GetMonitorInfo(hMonitor, &monInfo);

	if(monInfo.dwFlags == MONITORINFOF_PRIMARY) {
		screenSize->width = monInfo.rcMonitor.right - monInfo.rcMonitor.left;
		screenSize->height = monInfo.rcMonitor.bottom - monInfo.rcMonitor.top;
		return FALSE;
	} else {
		return TRUE;
	}

	//monitors.push_back(monInfo.rcMonitor);

}